//
//  ILGLiveToolManager.m
//  IfengLiveRecord
//
//  Created by zhangtongle-Pro on 2017/9/8.
//  Copyright © 2017年 凤凰新媒体. All rights reserved.
//

#import "ILGLiveToolManager.h"

#import "ILGALLiveTool.h"
#import "ILGJSLiveTool.h"

@interface ILGLiveToolManager ()
@property (nonatomic, strong) ILGLiveTool *liveTool;
@property (nonatomic, assign) CGFloat beautyValue; //当前美颜值
@property (nonatomic, assign) AVCaptureDevicePosition cameraPosition;


@property (nonatomic, strong) UIView *currentPreview;
@property (nonatomic, assign) UIInterfaceOrientation currentPreviewOrientation;

@property (nonatomic, assign) BOOL isPreviewing;
@end

@implementation ILGLiveToolManager

static ILGLiveToolManager *sharedInstance = nil;
+ (instancetype)managerWithLiveToolType:(ILGLiveToolType)liveToolType
{
    if (!sharedInstance) {
        sharedInstance = [[ILGLiveToolManager alloc] initWithLiveToolType:liveToolType];
    }
    return sharedInstance;
}

- (void)configureStreamListener:(id<ILGLiveToolDelegate>)listener
{
    self.liveTool.delegate = listener;
}

- (void)startPreview:(UIView *)view
{
    [self startPreview:view inDirection:UIInterfaceOrientationPortrait];
}
- (void)startPreview:(UIView *)view inDirection:(UIInterfaceOrientation)direction
{
    self.currentPreview = view;
    self.currentPreviewOrientation = direction;
    
    self.isPreviewing = YES;

    [self.liveTool startPreview:view inDirection:direction];
    
    // 用户之前设置过摄像头方向
    if (self.cameraPosition != AVCaptureDevicePositionUnspecified) {
        if (self.cameraPosition != self.liveTool.cameraPosition) {
            [self.liveTool switchCamera];
        }
    }
    
    // 用户之前设置过美颜值
    if (self.beautyValue != 0) {
        [self adjustBeautyValue:self.beautyValue];
    }
}
- (void)stopPreview
{
    if (self.isPreviewing) {
        self.isPreviewing = NO;
        [self.liveTool stopPreview];
    }
}

- (void)startPush:(NSString *)urlString
{
    [self.liveTool startPush:urlString];
}
- (void)pausePush
{
    [self.liveTool pausePush];
}
- (void)stopPush
{
    self.isPreviewing = NO;
    
    [self.liveTool stopPush];
    self.liveTool = nil;
}

- (void)switchCamera
{
    [self.liveTool switchCamera];
    self.cameraPosition = self.liveTool.cameraPosition;
}

- (void)adjustBeautyValue:(CGFloat)value
{
    [self.liveTool adjustBeautyValue:value];
    self.beautyValue = value;
}
- (CGFloat)currentBeautyValue
{
    return self.liveTool.beautyValue;
}

- (double)videoUploadBitrate
{
    return [self.liveTool videoUploadBitrate];
}


#pragma mark - private method

- (instancetype)initWithLiveToolType:(ILGLiveToolType)liveToolType {
    self = [super init];
    if (self == nil) {
        return nil;
    }
    
    if (liveToolType == ILGLiveToolTypeJinShan) {
        _liveTool = [[ILGJSLiveTool alloc] init];
    } else {
        _liveTool = [[ILGALLiveTool alloc] init];
    }
    
    return self;
}

- (void)clearData
{
    self.liveTool = ILGLiveToolTypeAli;
    self.beautyValue = 0.5;
    self.cameraPosition = AVCaptureDevicePositionUnspecified;
    self.currentPreview = nil;
    self.isPreviewing = NO;
    self.currentPreviewOrientation = UIInterfaceOrientationLandscapeLeft;
}

@end
